package org.rc.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.rc.dto.*;
import org.rc.entity.*;
import org.rc.enums.DevType;
import org.rc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dev")
public class DevController {

    @Autowired
    private SxjService sxjService;

    @Autowired
    private SxzjService sxzjService;

    @Autowired
    private SlpService slpService;

    @Autowired
    private ShspService shspService;

    @Autowired
    private SxzjChannelService sxzjChannelService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private RoomDevService roomDevService;

    @Autowired
    private UserRoomService userRoomService;

    @Autowired
    private UserService userService;

    @Autowired
    ConversationRecordService conversationRecordService;

    @Autowired
    MicService micService;

    /**
     * 添加摄像机
     * @param sxjAddDto
     * @return
     */
    @PutMapping("/sxj")
    public Res addSxj(@RequestBody SxjAddDto sxjAddDto){
        sxjService.addSxj(Sxj.getInstance(sxjAddDto));
        return Res.success();
    }

    @PostMapping("/sxj")
    public Res updateSxj(@RequestBody SxjUpdateDto sxjUpdateDto){
        sxjService.updateSxj(Sxj.getInstance(sxjUpdateDto));
        return Res.success();
    }

    @GetMapping("/sxjs")
    public Res getAllSxjs(){
        return Res.success(sxjService.getAllSxj());
    }

    @PostMapping("/sxj/page")
    public Res getSxjPage(@RequestBody SxjPageDto sxjPageDto){
        Page<Sxj> sxjPage = sxjService.getSxjPage(sxjPageDto);
        List<Sxj> sxjs = sxjPage.getRecords();
        List<SxjPageRes> sxjPageRes = new ArrayList<>();
        Map<Long,RoomDev> roomDevMap = null;
        Map<Long,Room> roomMap = null;
        if(!CollectionUtils.isEmpty(sxjs)){
            List<RoomDev> roomDevs = roomDevService.getBindRoomDevByTypeAndDevIds(DevType.SXJ.getType(),sxjs.stream().map(Sxj :: getId)
                    .collect(Collectors.toList()));
            if(!CollectionUtils.isEmpty(roomDevs)){
                List<Room> rooms = roomService.getByRoomIds(roomDevs.stream().map(RoomDev :: getRoomId).collect(Collectors.toList()));
                roomMap = rooms.stream().collect(Collectors.toMap(Room :: getId,room -> room));
                roomDevMap = roomDevs.stream().collect(Collectors.toMap(RoomDev :: getDevId, roomDev -> roomDev));
            }
            for(Sxj sxj : sxjs){
                SxjPageRes sxjPageRess = SxjPageRes.getInstance(sxj);
                if(!CollectionUtils.isEmpty(roomDevMap)){
                    Long id = sxj.getId();
                    if(roomDevMap.containsKey(id)){
                        Long roomId = roomDevMap.get(sxj.getId()).getRoomId();
                        if(roomMap.containsKey(roomId)){
                            sxjPageRess.setRoomName(roomMap.get(roomId).getName());
                        }
                    }
                }
                sxjPageRes.add(sxjPageRess);
            }
        }
        PageDto pageDto = PageDto.of(sxjPage);
        pageDto.setData(sxjPageRes);
        return Res.success(pageDto);
    }

    @DeleteMapping("/sxj/{id}")
    public Res removeSxj(@PathVariable(value = "id") Long id){
        sxjService.deleteSxj(id);
        sxzjChannelService.deleteBySxjIds(Arrays.asList(id));
        roomDevService.deleteByTypeAndId(DevType.SXJ.getType(), id);
        return Res.success();
    }

    @DeleteMapping("/sxjs")
    public Res batchRemoveSxj(@RequestBody  SxjBatchRemoveDto sxjBatchRemoveDto){
        sxjService.batchDeleteSxj(sxjBatchRemoveDto.getIds());
        sxzjChannelService.deleteBySxjIds(sxjBatchRemoveDto.getIds());
        roomDevService.deleteByTypeAndIds(DevType.SXJ.getType(), sxjBatchRemoveDto.getIds());
        return Res.success();
    }

    @GetMapping("/unbind/sxjs/{roomId}")
    public Res getUnbindSxjs(@PathVariable(value = "roomId") Long roomId){
        List<UnBindDevAndSelfBindDevRes> unBindSxjsRes = new ArrayList<>();
        List<Sxj> sxjs = sxjService.getAllSxj();
        if(!CollectionUtils.isEmpty(sxjs)){
            unBindSxjsRes = sxjs.stream().map(UnBindDevAndSelfBindDevRes:: getInstance).collect(Collectors.toList());
        }
        List<RoomDev> bindSxjs= roomDevService.getBindRoomDev(1);
        List<UnBindDevAndSelfBindDevRes> unbindSxj = new ArrayList<>();
        if(CollectionUtil.isEmpty(bindSxjs)){
            return Res.success(unBindSxjsRes);
        }
        Map<Long,RoomDev> roomDevMap = bindSxjs.stream().collect(Collectors.toMap(RoomDev :: getDevId,roomDev -> roomDev));
        for(UnBindDevAndSelfBindDevRes unBindSxjsRe : unBindSxjsRes){
            Long id = unBindSxjsRe.getId();
            if(!roomDevMap.containsKey(id)){
                unbindSxj.add(unBindSxjsRe);
            }else{
                if(Objects.equals(roomDevMap.get(id).getRoomId(), roomId)){
                    unBindSxjsRe.setSelected(true);
                    unbindSxj.add(unBindSxjsRe);
                }
            }
        }
        return Res.success(unbindSxj);
    }
    /**
     * 添加审讯主机
     * @param sxzjAddDto
     * @return
     */
    @PutMapping("/sxzj")
    public Res addSXZJ(@RequestBody SxzjAddDto sxzjAddDto){
        Sxzj sxzj = Sxzj.getInstance(sxzjAddDto);
        sxzjService.addSxzj(sxzj);
        List<SxzjAddDto.Channel> channels = sxzjAddDto.getChannels();
        if(CollectionUtil.isNotEmpty(channels)){
            sxzjChannelService.addSxzjChannels(channels.stream().map(channel -> {
                SxzjChannel sxzjChannel = new SxzjChannel();
                sxzjChannel.setSxzjId(sxzj.getId());
                sxzjChannel.setName(channel.getName());
                sxzjChannel.setChannelNo(channel.getChannelNo());
                sxzjChannel.setRate(channel.getRate());
                return sxzjChannel;
            }).collect(Collectors.toList()));
        }
        return Res.success();
    }

    @PostMapping("/sxzj")
    public Res updateSxzj(@RequestBody SxzjUpdateDto sxzjUpdateDto){
        sxzjService.updateSxzj(Sxzj.getInstance(sxzjUpdateDto));
        sxzjChannelService.deleteChannelsBySxzjId(sxzjUpdateDto.getId());
        List<SxzjUpdateDto.Channel> channels = sxzjUpdateDto.getChannels();
        if(CollectionUtil.isNotEmpty(channels)){
            sxzjChannelService.addSxzjChannels(channels.stream().map(channel -> {
                SxzjChannel sxzjChannel = new SxzjChannel();
                sxzjChannel.setSxzjId(sxzjUpdateDto.getId());
                sxzjChannel.setName(channel.getName());
                sxzjChannel.setChannelNo(channel.getChannelNo());
                sxzjChannel.setRate(channel.getRate());
                return sxzjChannel;
            }).collect(Collectors.toList()));
        }
        return Res.success();
    }

    @PostMapping("/sxzj/page")
    public Res getSxzjPage(@RequestBody SxzjPageDto sxzjPageDto){
        Page<Sxzj> sxzjPage = sxzjService.getSxzjPage(sxzjPageDto);
        List<Sxzj> sxzjs = sxzjPage.getRecords();
        List<SxzjPageRes> sxzjPageRes = new ArrayList<>();
        Map<Long,RoomDev> roomDevMap = null;
        Map<Long,Room> roomMap = null;
        if(!CollectionUtils.isEmpty(sxzjs)){
            List<RoomDev> roomDevs = roomDevService.getBindRoomDevByTypeAndDevIds(DevType.XSZJ.getType(),sxzjs.stream().map(Sxzj :: getId)
                    .collect(Collectors.toList()));
            if(!CollectionUtils.isEmpty(roomDevs)){
                List<Room> rooms = roomService.getByRoomIds(roomDevs.stream().map(RoomDev :: getRoomId).collect(Collectors.toList()));
                roomMap = rooms.stream().collect(Collectors.toMap(Room :: getId,room -> room));
                roomDevMap = roomDevs.stream().collect(Collectors.toMap(RoomDev :: getDevId, roomDev -> roomDev));
            }
            for(Sxzj sxzj : sxzjs){
                SxzjPageRes sxzjPageRe = SxzjPageRes.getInstance(sxzj);
                if(!CollectionUtils.isEmpty(roomDevMap)){
                    Long id = sxzj.getId();
                    if(roomDevMap.containsKey(id)){
                        Long roomId = roomDevMap.get(id).getRoomId();
                        if(roomMap.containsKey(roomId)){
                            sxzjPageRe.setRoomName(roomMap.get(roomId).getName());
                        }
                    }
                }
                sxzjPageRes.add(sxzjPageRe);
            }
        }
        PageDto pageDto = PageDto.of(sxzjPage);
        pageDto.setData(sxzjPageRes);
        return Res.success(pageDto);
    }

    @DeleteMapping("/sxzj/{id}")
    public Res removeSxzj(@PathVariable(value = "id") Long id){
        sxzjService.deleteSxzj(id);
        sxzjChannelService.deleteChannelsBySxzjId(id);
        roomDevService.deleteByTypeAndId(DevType.XSZJ.getType(), id);
        return Res.success();
    }

    @DeleteMapping("/sxzjs")
    public Res batchRemoveSxzj(@RequestBody  SxzjBatchRemoveDto sxzjBatchRemoveDto){
        sxzjService.batchDeleteSxzj(sxzjBatchRemoveDto.getIds());
        sxzjChannelService.deleteBySxzjIds(sxzjBatchRemoveDto.getIds());
        roomDevService.deleteByTypeAndIds(DevType.XSZJ.getType(), sxzjBatchRemoveDto.getIds());
        return Res.success();
    }

    @GetMapping("/sxzj/{id}")
    public Res getSxzj(@PathVariable(value = "id") Long id){
        Sxzj sxzj = sxzjService.getSxzjById(id);
        SxzjDetailRes sxzjDetailRes = SxzjDetailRes.getInstance(sxzj);
        List<SxzjChannel> channels = sxzjChannelService.sxzjChannels(sxzj.getId());
        sxzjDetailRes.setChannels(channels.stream().map(SxzjDetailRes.Channel :: getInstance).collect(Collectors.toList()));
        return Res.success(sxzjDetailRes);
    }

    @GetMapping("/sxzjs")
    public Res getUnbindSxzjs(){
        List<Sxzj> sxzjs = sxzjService.getAllSxzj();
        List<RoomDev> bindSxzjs= roomDevService.getBindRoomDev(2);
        List<Sxzj> unbindSxzj = new ArrayList<>();
        if(CollectionUtil.isEmpty(bindSxzjs)){
            return Res.success(sxzjs);
        }
        List<Long> sxzjIds  = bindSxzjs.stream().map(RoomDev :: getDevId).collect(Collectors.toList());
        for(Sxzj sxzj : sxzjs){
            if(!sxzjIds.contains(sxzj.getId())){
                unbindSxzj.add(sxzj);
            }
        }
        return Res.success(unbindSxzj);
    }

    @GetMapping("/unbind/sxzjs/{roomId}")
    public Res getUnbindSxzjs(@PathVariable(value = "roomId") Long roomId){
        List<UnBindDevAndSelfBindDevRes> unBindDevAndSelfBindDevRes = new ArrayList<>();
        List<Sxzj> sxzjs = sxzjService.getAllSxzj();
        if(!CollectionUtils.isEmpty(sxzjs)){
            unBindDevAndSelfBindDevRes = sxzjs.stream().map(UnBindDevAndSelfBindDevRes:: getInstance).collect(Collectors.toList());
        }
        List<RoomDev> bindSxzjs= roomDevService.getBindRoomDev(2);
        List<UnBindDevAndSelfBindDevRes> unbindSxzj = new ArrayList<>();
        if(CollectionUtil.isEmpty(bindSxzjs)){
            return Res.success(unBindDevAndSelfBindDevRes);
        }
        Map<Long,RoomDev> roomDevMap = bindSxzjs.stream().collect(Collectors.toMap(RoomDev :: getDevId,roomDev -> roomDev));
        for(UnBindDevAndSelfBindDevRes unBindSxjsRe : unBindDevAndSelfBindDevRes){
            Long id = unBindSxjsRe.getId();
            if(!roomDevMap.containsKey(id)){
                unbindSxzj.add(unBindSxjsRe);
            }else{
                if(Objects.equals(roomDevMap.get(id).getRoomId(), roomId)){
                    unBindSxjsRe.setSelected(true);
                    unbindSxzj.add(unBindSxjsRe);
                }
            }
        }
        return Res.success(unbindSxzj);
    }

    /**
     * 添加受令屏
     * @param slpAddDto
     * @return
     */
    @PutMapping("/slp")
    public Res addSlp(@RequestBody SlpAddDto slpAddDto){
        slpService.addSlp(Slp.getInstance(slpAddDto));
        return Res.success();
    }

    @PostMapping("/slp")
    public Res updateSlp(@RequestBody SlpUpdateDto slpUpdateDto){
        slpService.updateSlp(Slp.getInstance(slpUpdateDto));
        return Res.success();
    }

    @PostMapping("/slp/page")
    public Res getSlpPage(@RequestBody SlpPageDto slpPageDto){
        Page<Slp> slpPage = slpService.getSlpPage(slpPageDto);
        List<Slp> slps = slpPage.getRecords();
        List<SlpPageRes> slpPageRes = new ArrayList<>();
        Map<Long,RoomDev> roomDevMap = null;
        Map<Long,Room> roomMap = null;
        if(!CollectionUtils.isEmpty(slps)){
            List<RoomDev> roomDevs = roomDevService.getBindRoomDevByTypeAndDevIds(DevType.SLP.getType(),slps.stream().map(Slp :: getId)
                    .collect(Collectors.toList()));
            if(!CollectionUtils.isEmpty(roomDevs)){
                List<Room> rooms = roomService.getByRoomIds(roomDevs.stream().map(RoomDev :: getRoomId).collect(Collectors.toList()));
                roomMap = rooms.stream().collect(Collectors.toMap(Room :: getId,room -> room));
                roomDevMap = roomDevs.stream().collect(Collectors.toMap(RoomDev :: getDevId, roomDev -> roomDev));
            }
            for(Slp slp : slps){
                SlpPageRes slpPageRe = SlpPageRes.getInstance(slp);
                if(!CollectionUtils.isEmpty(roomDevMap)){
                    Long id = slp.getId();
                    if(roomDevMap.containsKey(id)){
                        Long roomId = roomDevMap.get(id).getRoomId();
                        if(roomMap.containsKey(roomId)){
                            slpPageRe.setRoomName(roomMap.get(roomId).getName());
                        }
                    }
                }
                slpPageRes.add(slpPageRe);
            }
        }
        PageDto pageDto = PageDto.of(slpPage);
        pageDto.setData(slpPageRes);
        return Res.success(pageDto);
    }

    @DeleteMapping("/slp/{id}")
    public Res removeSlp(@PathVariable(value = "id") Long id){
        slpService.deleteSLp(id);
        roomDevService.deleteByTypeAndId(DevType.SLP.getType(),id);
        return Res.success();
    }

    @DeleteMapping("/slps")
    public Res batchRemoveSlp(@RequestBody  SlpBatchRemoveDto slpBatchRemoveDto){
        slpService.batchDeleteSlp(slpBatchRemoveDto.getIds());
        roomDevService.deleteByTypeAndIds(DevType.SLP.getType(), slpBatchRemoveDto.getIds());
        return Res.success();
    }


    @GetMapping("/slps")
    public Res getUnbindSlps(){
        List<Slp> slps = slpService.getAllSlp();
        List<RoomDev> bindSlps= roomDevService.getBindRoomDev(3);
        List<Slp> unbindSlp = new ArrayList<>();
        if(CollectionUtil.isEmpty(bindSlps)){
            return Res.success(slps);
        }
        List<Long> slpIds  = bindSlps.stream().map(RoomDev :: getDevId).collect(Collectors.toList());
        for(Slp slp : slps){
            if(!slpIds.contains(slp.getId())){
                unbindSlp.add(slp);
            }
        }
        return Res.success(unbindSlp);
    }

    @GetMapping("/unbind/slps/{roomId}")
    public Res getUnbindSlps(@PathVariable(value = "roomId") Long roomId){
        List<UnBindDevAndSelfBindDevRes> unBindDevAndSelfBindDevRes = new ArrayList<>();
        List<Slp> slps = slpService.getAllSlp();
        if(!CollectionUtils.isEmpty(slps)){
            unBindDevAndSelfBindDevRes = slps.stream().map(UnBindDevAndSelfBindDevRes:: getInstance).collect(Collectors.toList());
        }
        List<RoomDev> bindSlps= roomDevService.getBindRoomDev(3);
        List<UnBindDevAndSelfBindDevRes> unbindSlp = new ArrayList<>();
        if(CollectionUtil.isEmpty(bindSlps)){
            return Res.success(unBindDevAndSelfBindDevRes);
        }
        Map<Long,RoomDev> roomDevMap = bindSlps.stream().collect(Collectors.toMap(RoomDev :: getDevId,roomDev -> roomDev));
        for(UnBindDevAndSelfBindDevRes unBindSxjsRe : unBindDevAndSelfBindDevRes){
            Long id = unBindSxjsRe.getId();
            if(!roomDevMap.containsKey(id)){
                unbindSlp.add(unBindSxjsRe);
            }else{
                if(Objects.equals(roomDevMap.get(id).getRoomId(), roomId)){
                    unBindSxjsRe.setSelected(true);
                    unbindSlp.add(unBindSxjsRe);
                }
            }
        }
        return Res.success(unbindSlp);
    }

    /**
     * 添加神火视频
     * @param shspAddDto
     * @return
     */
    @PutMapping("/shsp")
    public Res addShsp(@RequestBody ShspAddDto shspAddDto){
        shspService.addShsp(Shsp.getInstance(shspAddDto));
        return Res.success();
    }
    @PostMapping("/shsp")
    public Res updateSxzj(@RequestBody ShspUpdateDto shspUpdateDto){
        shspService.updateShsp(Shsp.getInstance(shspUpdateDto));
        return Res.success();
    }

    @PostMapping("/shsp/page")
    public Res getSxzjPage(@RequestBody ShspPageDto shspPageDto){
        Page<Shsp> shspPage = shspService.getShspPage(shspPageDto);
        List<Shsp> shsps = shspPage.getRecords();
        List<ShspPageRes> shspPageRes = new ArrayList<>();
        Map<Long,RoomDev> roomDevMap = null;
        Map<Long,Room> roomMap = null;
        if(!CollectionUtils.isEmpty(shsps)){
            List<RoomDev> roomDevs = roomDevService.getBindRoomDevByTypeAndDevIds(DevType.SHSP.getType(),shsps.stream().map(Shsp :: getId)
                    .collect(Collectors.toList()));
            if(!CollectionUtils.isEmpty(roomDevs)){
                List<Room> rooms = roomService.getByRoomIds(roomDevs.stream().map(RoomDev :: getRoomId).collect(Collectors.toList()));
                roomMap = rooms.stream().collect(Collectors.toMap(Room :: getId,room -> room));
                roomDevMap = roomDevs.stream().collect(Collectors.toMap(RoomDev :: getDevId, roomDev -> roomDev));
            }
            for(Shsp shsp : shsps){
                ShspPageRes shspPageRe = ShspPageRes.getInstance(shsp);
                if(!CollectionUtils.isEmpty(roomDevMap)){
                    Long id = shsp.getId();
                    if(roomDevMap.containsKey(id)){
                        Long roomId = roomDevMap.get(id).getRoomId();
                        if(roomMap.containsKey(roomId)){
                            shspPageRe.setRoomName(roomMap.get(roomId).getName());
                        }
                    }
                }
                shspPageRes.add(shspPageRe);
            }
        }
        PageDto pageDto = PageDto.of(shspPage);
        pageDto.setData(shspPageRes);
        return Res.success(pageDto);
    }

    @DeleteMapping("/shsp/{id}")
    public Res removeShsp(@PathVariable(value = "id") Long id){
        shspService.deleteShsp(id);
        roomDevService.deleteByTypeAndId(DevType.SHSP.getType(),id);
        return Res.success();
    }

    @DeleteMapping("/shsps")
    public Res batchRemoveShsp(@RequestBody  ShspBatchRemoveDto shspBatchRemoveDto){
        shspService.batchDeleteShsp(shspBatchRemoveDto.getIds());
        roomDevService.deleteByTypeAndIds(DevType.SHSP.getType(),shspBatchRemoveDto.getIds());
        return Res.success();
    }

    @GetMapping("/shsps")
    public Res getUnbindShsps(){
        List<Shsp> shsps = shspService.getAllShsps();
        List<RoomDev> bindShsps= roomDevService.getBindRoomDev(4);
        List<Shsp> unbindShsp = new ArrayList<>();
        if(CollectionUtil.isEmpty(bindShsps)){
            return Res.success(shsps);
        }
        List<Long> shspIds  = bindShsps.stream().map(RoomDev :: getDevId).collect(Collectors.toList());
        for(Shsp shsp : shsps){
            if(!shspIds.contains(shsp.getId())){
                unbindShsp.add(shsp);
            }
        }
        return Res.success(unbindShsp);
    }


    @GetMapping("/unbind/shsps/{roomId}")
    public Res getUnbindShsps(@PathVariable(value = "roomId") Long roomId){
        List<UnBindDevAndSelfBindDevRes> unBindDevAndSelfBindDevRes = new ArrayList<>();
        List<Shsp> shsps = shspService.getAllShsps();
        if(!CollectionUtils.isEmpty(shsps)){
            unBindDevAndSelfBindDevRes = shsps.stream().map(UnBindDevAndSelfBindDevRes:: getInstance).collect(Collectors.toList());
        }
        List<RoomDev> bindShsps= roomDevService.getBindRoomDev(4);
        List<UnBindDevAndSelfBindDevRes> unbindShsp = new ArrayList<>();
        if(CollectionUtil.isEmpty(bindShsps)){
            return Res.success(unBindDevAndSelfBindDevRes);
        }
        Map<Long,RoomDev> roomDevMap = bindShsps.stream().collect(Collectors.toMap(RoomDev :: getDevId,roomDev -> roomDev));
        for(UnBindDevAndSelfBindDevRes unBindSxjsRe : unBindDevAndSelfBindDevRes){
            Long id = unBindSxjsRe.getId();
            if(!roomDevMap.containsKey(id)){
                unbindShsp.add(unBindSxjsRe);
            }else{
                if(Objects.equals(roomDevMap.get(id).getRoomId(), roomId)){
                    unBindSxjsRe.setSelected(true);
                    unbindShsp.add(unBindSxjsRe);
                }
            }
        }
        return Res.success(unbindShsp);
    }

    @PutMapping("/mic")
    public Res addMic(@RequestBody MicAddDto micAddDto){
        micService.addMic(Mic.getInstance(micAddDto));
        return Res.success();
    }

    @PostMapping("/mic")
    public Res updateMic(@RequestBody MicUpdateDto micUpdateDto){
        micService.updateByMicId(Mic.getInstance(micUpdateDto));
        return Res.success();
    }

    @PostMapping("/mic/page")
    public Res getMicPage(@RequestBody MicPageDto micPageDto){
        Page<Mic> micPage = micService.getMicPage(micPageDto);
        List<Mic> mics = micPage.getRecords();
        List<MicPageRes> micPageRes = new ArrayList<>();
        Map<Long,RoomDev> roomDevMap = null;
        Map<Long,Room> roomMap = null;
        if(!CollectionUtils.isEmpty(mics)){
            List<RoomDev> roomDevs = roomDevService.getBindRoomDevByTypeAndDevIds(DevType.MIC.getType(),mics.stream().map(Mic :: getId)
                    .collect(Collectors.toList()));
            if(!CollectionUtils.isEmpty(roomDevs)){
                List<Room> rooms = roomService.getByRoomIds(roomDevs.stream().map(RoomDev :: getRoomId).collect(Collectors.toList()));
                roomMap = rooms.stream().collect(Collectors.toMap(Room :: getId,room -> room));
                roomDevMap = roomDevs.stream().collect(Collectors.toMap(RoomDev :: getDevId, roomDev -> roomDev));
            }
            for(Mic mic : mics){
                MicPageRes micPageRe = MicPageRes.getInstance(mic);
                if(!CollectionUtils.isEmpty(roomDevMap)){
                    Long id = mic.getId();
                    if(roomDevMap.containsKey(id)){
                        Long roomId = roomDevMap.get(id).getRoomId();
                        if(roomMap.containsKey(roomId)){
                            micPageRe.setRoomName(roomMap.get(roomId).getName());
                        }
                    }
                }
                micPageRes.add(micPageRe);
            }
        }
        PageDto pageDto = PageDto.of(micPage);
        pageDto.setData(micPageRes);
        return Res.success(pageDto);
    }

    @DeleteMapping("/mic/{id}")
    public Res removeMic(@PathVariable(value = "id") Long id){
        micService.deleteById(id);
        roomDevService.deleteByTypeAndId(DevType.MIC.getType(),id);
        return Res.success();
    }

    @DeleteMapping("/mics")
    public Res batchRemoveMisc(@RequestBody  MicBatchRemoveDto micBatchRemoveDto){
        micService.batchDeleteMic(micBatchRemoveDto.getIds());
        roomDevService.deleteByTypeAndIds(DevType.MIC.getType(),micBatchRemoveDto.getIds());
        return Res.success();
    }

    @GetMapping("/unbind/mics/{roomId}")
    public Res getUnbindMics(@PathVariable(value = "roomId") Long roomId){
        List<UnBindDevAndSelfBindDevRes> unBindDevAndSelfBindDevRes = new ArrayList<>();
        List<Mic> mics = micService.getAllMics();
        if(!CollectionUtils.isEmpty(mics)){
            unBindDevAndSelfBindDevRes = mics.stream().map(UnBindDevAndSelfBindDevRes:: getInstance).collect(Collectors.toList());
        }
        List<RoomDev> bindMics= roomDevService.getBindRoomDev(5);
        List<UnBindDevAndSelfBindDevRes> unbindMic = new ArrayList<>();
        if(CollectionUtil.isEmpty(bindMics)){
            return Res.success(unBindDevAndSelfBindDevRes);
        }
        Map<Long,RoomDev> roomDevMap = bindMics.stream().collect(Collectors.toMap(RoomDev :: getDevId,roomDev -> roomDev));
        for(UnBindDevAndSelfBindDevRes unBindSxjsRe : unBindDevAndSelfBindDevRes){
            Long id = unBindSxjsRe.getId();
            if(!roomDevMap.containsKey(id)){
                unbindMic.add(unBindSxjsRe);
            }else{
                if(Objects.equals(roomDevMap.get(id).getRoomId(), roomId)){
                    unBindSxjsRe.setSelected(true);
                    unbindMic.add(unBindSxjsRe);
                }
            }
        }
        return Res.success(unbindMic);
    }
}
